package app.auto.runner.base;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.littlejerk.rvdivider.builder.XLinearBuilder;

import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.RecyclerView;
import app.auto.runner.base.intf.FunCallback;
import app.auto.runner.base.intf.json.JsonToMapUtils;
import app.auto.runner.base.intf.json.JsonUtil;

public class Json2SingleOptions {
    public static RecycleViewAdapter parseJson(String json, RecyclerView recycleView, List list2, FunCallback callback) {
        RResApp.initR(recycleView.getContext(), recycleView.getContext().getPackageName());
        RRes.initR(recycleView.getContext(), "com.aliang.auto");

        SPrefUtil.iniContext(recycleView.getContext());

        String id_right_choose = (String) app.auto.runner.base.intf.json.JsonUtil.findJsonLink("xml-id_right_choose", json);
        String id_left_display = (String) app.auto.runner.base.intf.json.JsonUtil.findJsonLink("xml-id_left_display", json);
        String id_spec_choose = (String) app.auto.runner.base.intf.json.JsonUtil.findJsonLink("xml-id_spec_choose", json);
        String id_click_cnt = (String) app.auto.runner.base.intf.json.JsonUtil.findJsonLink("xml-id_click_cnt", json);

        String unselect = (String) app.auto.runner.base.intf.json.JsonUtil.findJsonLink("xml-unselect", json);
        String select = (String) app.auto.runner.base.intf.json.JsonUtil.findJsonLink("xml-select", json);
        String cnt_layout = (String) app.auto.runner.base.intf.json.JsonUtil.findJsonLink("xml-cnt_layout", json);
        String select_default = (String) app.auto.runner.base.intf.json.JsonUtil.findJsonLink("xml-select_default", json);

        String data = (String) JsonUtil.findJsonLink("data", json).toString();

        List list = JsonToMapUtils.jsontoList(data);
        list2.addAll(list);

        ((Map) list2.get(Integer.parseInt(select_default))).put(id_right_choose, select);


//        List list2 = ListBuilder.build().add(MapBuilder.build().add(id_left_display, "11").get())
//                .add(MapBuilder.build().add(id_left_display, "22").add("ok", "R.mipmap.auto_arrow_left_black").get())
//                .add(MapBuilder.build().add(id_left_display, "33").get())
//                .get();

        int val = ResUtil.getAndroidValue(cnt_layout);
        int cnt_id = ResUtil.getAndroidValue(id_click_cnt);

        RecycleViewAdapter mapAdapter2 = RecycleViewAdapter.build(recycleView.getContext())

                .setListviewItemLayoutId(val)
                .setViewIds(com.aliang.auto.R.id.name, com.aliang.auto.R.id.ok)
                .setObjectFields(id_left_display, id_right_choose)
                .setItemDataSrc(list2)
                .setRecyclerView(recycleView).get();


        mapAdapter2.addActionListener(new RecycleViewAdapter.ActionListener(cnt_id, RecycleViewAdapter.ActionListener.OnClick) {
            @Override
            public void handle(RecycleViewAdapter mapAdapter, View view, int pos, ListenerBoxAdaptRecycle ListenerBoxAdaptRecycle, Object retainedObj) {
                ListMapChooser.choose(list2, pos, id_right_choose, unselect, select);
                mapAdapter2.notifyDataSetChanged();
                SPrefUtil.putValue(
                        recycleView.getId() + "_pos", "" + pos);
                SPrefUtil.putValue(
                        recycleView.getId() + "_selection", JsonToMapUtils.mapToJson((Map) list2.get(pos)));
                callback.onCallback((Map) list2.get(pos),null);
            }
        });

        recycleView.addItemDecoration(
                new XLinearBuilder(recycleView.getContext())
                        .setLeftPadding(13f)
                        .setRightPadding(13f)
                        .setShowFirstTopLine(false)
                        .setShowLastLine(false)
                        .setColor(Color.parseColor("#f2f2f2"))
                        .build()
        );
        String pos =
                (String) SPrefUtil.getValue(
                        recycleView.getId() + "_pos");

        if (!TextUtils.isEmpty(pos)) {
            int intpos = Integer.parseInt(pos);
            ListMapChooser.choose(list2, intpos, id_right_choose, unselect, select);
            mapAdapter2.notifyDataSetChanged();
        }
        return mapAdapter2;
    }
}
